/**
 * Created by WT-SUN on 2017/5/22.
 */
$(function () {
    $('#cerTb').datagrid({
        rownumbers: true,//显示行号
        striped: true,
        fit: true,
        singleSelect: true,
        sortOrder: true,
        fitColumns: true,
        url:ROOT_PATH + '/threeSimultaneities/report/rTable.shtml',
        pagination: true,
        toolbar: '#toolbar',
        queryParams: {
            sort: 'addTime',
            order: 'desc'
        },
        columns: [[
            {
                field: 'id',checkbox:true
            },
            {field: 'name', title: '月报', width: 300,formatter: function (value, row, index) {
                return "<a href='javascript:void(0)' class='easyui-linkbutton text-primary' plain='true'   onclick='infoReport(\""+row.id+"\");'>"+row.name+"</a>";
            }},
            {field: 'submitState', title: '状态', width: 100,formatter: function (value, row, index) {
                return row.submitState=='1'?'已提交':'未提交';
            }},
            {field: 'deptName', title: '登记部门', width: 100},
            {field: 'realName', title: '登记人', width: 100},
            {field: 'addTime', title: '登记时间', width: 100}
        ]]
    });
    $('#btn_th_report_add').on('click',function () {
        addReport();
    });
    $('#btn_th_report_delete').on('click',function () {
        deleteReport();
    });
    $('#btn_tr_report_edit').on('click',function () {
        editReport();
    });
    $('#btn_advanced_search').on("click", function () {
        $('#queryForm2').toggle();
        $('#cerTb').datagrid('resize');
    });
    $('#btn_clear').on("click", function () {
        $('#queryForm1').form('reset');
        $('#queryForm2').form('reset');
    });
    
    var date = new Date();
    var year = date.getFullYear();
    var yearthData = new Array();
    yearthData.push({id:"",text:"请选择"});
    for(var i =0;i<6;i++){
        var y = {id:year-i,text:(year-i)+'年'};
        yearthData.push(y);
    }
    $('#year').combobox(
        {
            data:yearthData,
            valueField:'id',
            textField:'text',
            editable: false
        }
    );
    var monthArr=[];
    monthArr.push({id:"",text:"请选择"});
    for(var i =1;i<13;i++){
        var idv="0"+i;
        var y = {id:idv,text:i+"月"};
        monthArr.push(y);
    }
    $('#month').combobox(
        {
            data:monthArr,
            valueField:'id',
            textField:'text',
            editable: false
        }
    );
    
    $("#searchAddUser").textbox({
        iconWidth: 20,
        icons: [{
            iconCls: 'icon-add',
            handler: function(e){
                add_userId_func();
            }
        },{
            iconCls: 'icon-remove',
            handler: function(e){
                $(e.data.target).textbox('clear');

            }
        }]
    });
    queryDepartmentTree();

});
var add_userId_func = function () {
    WindowManager.openLayer({
        type: 2,
        title: '选择人员信息',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
        callBack:function(data){
            getSelectedUser(data);
        }
    });
};
function queryDepartmentTree(){
    $.ajax({
        url:ROOT_PATH + '/permission/department/getDepartmentTree.shtml',//提交地址
        data:{parentId : 0},//将表单数据序列化
        type:"POST",
        dataType:"json",
        success:function(result){
            deptCom('searchAddDept',result);
        }
    });
}
function deptCom(id,result) {
    var deptId='#'+id;
    $(deptId).combotree(
        {
            data:result,
            prompt:'请选择',
            require:true,
            loadFilter:function(data){
                var newData = new Array();
                newData.push({id:"",text:'请选择'});
                newData.push(data[0]);
                return newData;
            }
        }
    );
}
/**
 * 人员信息选择
 */
var getSelectedUser = function (data) {
    $("#searchAddUser").textbox('setValue', data[0].id).textbox('setText', data[0].realName);
};
function openLayers(title, content) {
    WindowManager.openLayer({
        type: 2,
        resize: false,
        title: title,
        area: ['1150px', '70%'],
        content: content,
        callBack:function(data){
            if(data){
                reLoadData();
            }
        }
    });
}
function reLoadData() {
    $('#cerTb').datagrid('clearSelections');
    var queryParams1 = $('#queryForm1').serializeJson();
    var queryParams2 = $('#queryForm2').serializeJson();
    var queryParams = $.extend(queryParams1,queryParams2);
    queryParams["sort"] = 'addTime';
    queryParams["order"] = 'desc';
    $('#cerTb').datagrid('options').queryParams = queryParams;
    $("#cerTb").datagrid('reload', ROOT_PATH + '/threeSimultaneities/report/rTable.shtml');
}
function addReport() {
    var title = '新增三同时月报';
    var content = ROOT_PATH + '/threeSimultaneities/report/add.shtml?resourceId=' + $.getQueryParam('resourceId');
    WindowManager.openLayer({
        type: 2,
        resize: false,
        title: title,
        area: ['430px', '40%'],
        content: content,
        callBack:function(data){
            if(data){
                reLoadData();
            }
        }
    });
}
function deleteReport() {
    var rows = $('#cerTb').datagrid('getSelections');

    if (rows.length == 0) {
        layer.alert("选择需要删除的行！");
        return;
    }
    var id = rows[0].id;
    layer.confirm('是否确定删除当前选中数据？', {
        btn: ['确定', '取消'] //按钮
    }, function () {
        $.ajax({
            type: 'post',
            url: ROOT_PATH + '/threeSimultaneities/report/deleteById.shtml',
            data: {id: id},
            dataType: "json",
            success: function (result) {
                //result = $.parseJSON(result);
                if (result.success) {
                    $('#planTb').datagrid('clearSelections');
                    layer.msg('删除成功', {
                        icon: 1,
                        time: 1000
                    });
                    reLoadData();
                }
                else {
                    layer.msg('删除失败', {
                        icon: 2,
                        time: 1000
                    });
                }
            }
        });
    });
}
function editReport() {
    var rows = $('#cerTb').datagrid('getSelections');
    if (rows.length == 0) {
        layer.alert("选择需要删除的行！");
        return;
    }
    var id = rows[0].id;
    var title = '三同时月报明细';
    var content = ROOT_PATH + '/threeSimultaneities/report/edit.shtml?resourceId=' + $.getQueryParam('resourceId')+'&id='+id;
    openLayers(title, content);
}

function infoReport(id) {
    var title = '三同时月报明细';
    var content = ROOT_PATH + '/threeSimultaneities/report/info.shtml?resourceId=' + $.getQueryParam('resourceId')+'&id='+id;
    openLayers(title, content);
}
